<template>
  <web-view v-if="webviewUsable" :src="url" @message="handleMessage" />
</template>

<script lang="ts">
import { ref, onMounted } from 'vue-demi';
import { getCurrentInstance } from '@tarojs/taro';
import { HOME } from '@/const/h5';
import Storage from '@/utils/storage';
import ParamsUtil from '@/utils/params';

export default {
  setup() {
    const webviewUsable = ref(false);
    const url = ref(HOME);

    onMounted(async () => {
      let link = getCurrentInstance().router?.params.redirect as string;
      link = decodeURIComponent(link);
      if (link.indexOf('?') > -1) {
        link = `${link}&v=${Date.now()}`;
      } else {
        link = `${link}?v=${Date.now()}`;
      }
      const params = ParamsUtil.getParams(link);
      const token = await Storage.getItem('token');

      url.value = ParamsUtil.addParams(link.split('?')[0], { ...params, token });
      webviewUsable.value = true;
    });

    const handleMessage = () => {};

    return {
      url,
      webviewUsable,
      handleMessage,
    };
  },
};
</script>
